package repo

import (
	"gorm.io/gorm"
	"sensi/app/model"
)

type Device struct {
}

func (Device) DevicesByPlatform(db *gorm.DB, platform int) ([]*model.Device, error) {
	devices := make([]*model.Device, 0)

	if platform != 0 {
		db = db.Where("platform = ?", platform)
	}
	err := db.Find(&devices).Error
	if err == gorm.ErrRecordNotFound {
		return devices, nil
	}
	return devices, err
}

func (Device) Devices(db *gorm.DB) ([]*model.Device, error) {
	devices := make([]*model.Device, 0)

	err := db.Find(&devices).Error
	if err == gorm.ErrRecordNotFound {
		return devices, nil
	}
	return devices, err
}

func (Device) GetByID(db *gorm.DB, id int) (*model.Device, error) {
	device := &model.Device{}
	err := db.First(device, id).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	return device, err
}
